﻿using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.Common.Utils;
using ED.Entity;
using ED.IRepository.Menu;
using ED.IService;
using ED.Model.Response.Menu;
using System.Collections.Generic;

namespace ED.Service.Menu
{
    public class MenuOperationService : BaseService,IMenuOperationService

    {
        private readonly IMenuRepository menuRepository;
        public MenuOperationService(IMenuRepository menuRepository)
        {
            this.menuRepository = menuRepository;
        }

        public Tuple<bool, string> Delete(int id)
        {
            if (!id.IsDefault())
            {
                bool isSuccess = menuRepository.Delete(t => t.Id == id) > 0;
                if (isSuccess)
                {
                    return Tuple.Create(false, "删除成功");
                }
                else
                {
                    return Tuple.Create(false, "删除失败");
                }
            }
            else
            {
                throw new Exception("编号不存在");
            }
        }

        public  ResponseOutput<List<MenuRoot>> GetMenuList(MenuReq req)
        {
           var menuList = Orm.Select<Base_MenuEntity>()
                .WhereIf(req.BelongSystem.HasValue && req.BelongSystem > 0, t => t.BelongSystem == req.BelongSystem)
                .WhereIf(req.Name.IsNotNullOrEmptyOrWhiteSpace(), t => t.Name.Contains(req.Name))
                .ToList();
            var resTreeList = menuList.FetchToTree(it => it.Id, it => it.ParentId, it => it.Children, it => it.ParentId == 0);
            return ResponseOutput.Ok(SetMenuList(resTreeList));
        }


        public ResponseOutput UpdateOrInsert(MenuReq menuReq)
        {
            bool isSuccess = false;
            if (menuReq.Id > 0)
            {

                var entity = menuReq.MapTo<Base_MenuEntity>();
                isSuccess = menuRepository.Update(entity)?.Id> 0;

            }
            else
            {
                var res = menuRepository.Get(t => t.Name == menuReq.Name || t.Title == menuReq.Title)?.Id;
                if (res.IsDefault())
                {
                    // 插入数据
                    var entity = menuReq.MapTo<Base_MenuEntity>();
                    isSuccess = menuRepository.Insert(entity)?.Id > 0;
                }
                else
                {
                    // 数据重复

                    return ResponseOutput.NotOk("数据重复");
                }
            }
            return ResponseOutput.Ok("编辑成功");
        }

        private List<MenuRoot> SetMenuList(List<Base_MenuEntity> data)
        {
            List <MenuRoot> resList = new List<MenuRoot>();
            foreach (var item in data)
            {
                var mentRoot_ = item.Mapper<MenuRoot>();
                if (item.Children.IsNotNullOrEmpty())
                {
                    _Recursion(item.Children, mentRoot_.Children);
                }
                mentRoot_.Meta = item.Mapper<Meta>();
                resList.Add(mentRoot_);
            }

           void _Recursion(List < Base_MenuEntity > children, List<MenuRoot> menuRoot)
            {
                foreach (var item in children)
                {
                    var cc = menuRoot.Where(t => t.Id == item.Id).FirstOrDefault();
                    if (item.Children.IsNotNullOrEmpty())
                    {
                        _Recursion(item.Children, cc.Children);
                    }
                    menuRoot.Where(t => t.Id == item.Id).FirstOrDefault().Meta = item.Mapper<Meta>();
                }
            }

            return resList;
        }
    }
}
